﻿Imports System.Collections.ObjectModel
Imports Microsoft.Win32

Public Class OptionsDialog
    Private m_res As UCBase.ResourceManager
    Private M_Plugins As String
    Private M_Macro As String
    Private M_Project As String
    Private M_Data As String
    Private M_Settings As String
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        m_res.DevelopmentMode = CheckBox1.Checked
    End Sub
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        OpenFileDialog1.Filter = "Plugin Set File *.Psf|*.Psf"
        If m_res.PluginSet = "" Then
            If m_res.SettingsPath > "" Then OpenFileDialog1.InitialDirectory = m_res.SettingsPath
        Else
            OpenFileDialog1.InitialDirectory = IO.Path.GetDirectoryName(m_res.PluginSet)
        End If
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            m_res.PluginSet = OpenFileDialog1.FileName
            LoadSet()
        End If
    End Sub
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        SaveFileDialog1.Filter = "Plugin Set File *.Psf|*.Psf"
        If m_res.PluginSet = "" Then
            If m_res.SettingsPath > "" Then SaveFileDialog1.InitialDirectory = m_res.SettingsPath
        Else
            SaveFileDialog1.FileName = m_res.PluginSet
        End If
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            m_res.PluginSet = SaveFileDialog1.FileName
            Label2.Text = SaveFileDialog1.FileName
            SaveSet()
        End If
    End Sub
    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        OpenFileDialog1.Filter = "Plugins *.Ucp|*.Ucp|Dll's *.Dll|*.Dll"
        If m_res.PluginPath > "" Then OpenFileDialog1.InitialDirectory = m_res.PluginPath
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim F As String = OpenFileDialog1.FileName
            Dim File As String = IO.Path.GetFileName(F)
            Dim Path As String = IO.Path.GetDirectoryName(F)
            Dim Desc As String = ""
            Dim Version As String = ""
            Dim Devs As String = ""
            Dim Plug As UCBase.I_Plugin
            Plug = LoadPlugIn(F)
            If Plug IsNot Nothing Then
                Dim Info As FileVersionInfo
                Info = FileVersionInfo.GetVersionInfo(F)
                Version = Info.ProductVersion
                Desc = Info.Comments
                Devs = Plug.Devices.Count.ToString
                Plug.Unload()
                Plug = Nothing
            End If
            Dim LVI As New ListViewItem(File)
            LVI.SubItems.Add(Desc)
            LVI.SubItems.Add(Version)
            LVI.SubItems.Add(Devs)
            LVI.SubItems.Add(Path)
            ListView1.Items.Add(LVI)
        End If
        SaveSet()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim I As Integer = ListView1.SelectedIndices(0)
        ListView1.Items.RemoveAt(I)
        SaveSet()
    End Sub
    Private Sub LoadSet()
        If m_res.PluginSet = "" Then Exit Sub
        Label2.Text = m_res.PluginSet
        Dim FS As New IO.FileStream(m_res.PluginSet, IO.FileMode.Open)
        Dim Reader As New IO.StreamReader(FS)
        ListView1.Items.Clear()
        Do While Reader.EndOfStream = False
            Dim F As String = Reader.ReadLine
            Dim File As String = IO.Path.GetFileName(F)
            Dim Path As String = IO.Path.GetDirectoryName(F)
            Dim Desc As String = ""
            Dim Version As String = ""
            Dim Devs As String = ""
            Dim Plug As UCBase.I_Plugin
            Plug = LoadPlugIn(F)
            If Plug IsNot Nothing Then
                Dim Info As FileVersionInfo
                Info = FileVersionInfo.GetVersionInfo(F)
                Version = Info.ProductVersion
                Desc = Info.Comments

                Devs = Plug.Devices.Count.ToString
                Plug.Unload()
                Plug = Nothing
            End If
            Dim LVI As New ListViewItem(File)
            LVI.SubItems.Add(Desc)
            LVI.SubItems.Add(Version)
            LVI.SubItems.Add(Devs)
            LVI.SubItems.Add(Path)
            ListView1.Items.Add(LVI)
        Loop
        Reader.Close()
    End Sub
    Private Sub ScanPluginFolder()
        Dim files As ReadOnlyCollection(Of String)
        If IO.Directory.Exists(m_res.PluginPath) Then
            files = My.Computer.FileSystem.GetFiles(m_res.PluginPath, FileIO.SearchOption.SearchAllSubDirectories, New String() {"*.ucp", "*.dll"})
            For Each F As String In files
                Dim File As String = IO.Path.GetFileName(F)
                Dim Path As String = IO.Path.GetDirectoryName(F)
                Dim Desc As String = ""
                Dim Version As String = ""
                Dim Devs As String = ""
                Dim Plug As UCBase.I_Plugin
                Plug = LoadPlugIn(F)
                If Plug IsNot Nothing Then
                    Dim Info As FileVersionInfo
                    Info = FileVersionInfo.GetVersionInfo(F)
                    Version = Info.ProductVersion
                    Desc = Info.Comments
                    Devs = Plug.Devices.Count.ToString
                    Plug.Unload()
                    Plug = Nothing
                End If
                Dim LVI As New ListViewItem(File)
                LVI.SubItems.Add(Desc)
                LVI.SubItems.Add(Version)
                LVI.SubItems.Add(Devs)
                LVI.SubItems.Add(Path)
                ListView2.Items.Add(LVI)
            Next
        End If
    End Sub
    Private Sub ScanLibFolder()
        Dim files As ReadOnlyCollection(Of String)
        Dim LibPath = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\UCS\Ide", "LibPath", "")

        If IO.Directory.Exists(LibPath) Then
            files = My.Computer.FileSystem.GetFiles(LibPath, FileIO.SearchOption.SearchAllSubDirectories, New String() {"*.dll", "*.dll"})
            For Each F As String In files
                Dim File As String = IO.Path.GetFileName(F)
                Dim Path As String = IO.Path.GetDirectoryName(F)
                Dim Desc As String = ""
                Dim Version As String = ""
                Dim Devs As String = ""
                'Dim Plug As UCBase.I_Plugin
                'Plug = LoadPlugIn(F)
                'If Plug IsNot Nothing Then
                Dim Info As FileVersionInfo
                Info = FileVersionInfo.GetVersionInfo(F)
                Version = Info.ProductVersion
                Desc = Info.Comments
                'Devs = Plug.Devices.Count.ToString
                'Plug.Unload()
                'Plug = Nothing
                'End If
                Dim LVI As New ListViewItem(File)
                LVI.SubItems.Add(Desc)
                LVI.SubItems.Add(Version)
                'LVI.SubItems.Add(Devs)
                LVI.SubItems.Add(Path)
                ListView3.Items.Add(LVI)
            Next
        End If
    End Sub
    Private Sub SaveSet()
        If Label2.Text = "" Then Exit Sub
        Dim FS As New System.IO.FileStream(Label2.Text, IO.FileMode.Create)
        Dim Writer As New IO.StreamWriter(FS)
        For Each I As ListViewItem In ListView1.Items
            Dim F As String
            Dim File As String = I.Text
            Dim Path As String = I.SubItems(4).Text
            F = IO.Path.Combine(Path, File)
            Writer.WriteLine(F)
        Next
        Writer.Close()
    End Sub
    Private Function LoadPlugIn(ByVal LoadPath As String) As UCBase.I_Plugin
        Dim NewPlugIn As UCBase.I_Plugin
        Try
            Dim PlugInAssembly As Reflection.Assembly = Reflection.Assembly.LoadFrom(LoadPath)
            Dim Types() As Type
            Dim FoundInterface As Type
            Types = PlugInAssembly.GetTypes
            For Each PlugInType As Type In Types
                FoundInterface = PlugInType.GetInterface("UCBase.I_Plugin")
                If FoundInterface IsNot Nothing Then
                    NewPlugIn = DirectCast(PlugInAssembly.CreateInstance(PlugInType.FullName), UCBase.I_Plugin)
                    Return NewPlugIn
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return Nothing
    End Function

    Private Sub OptionsDialog_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CheckBox1.Checked = m_res.ScanPluginFolder
        If m_res.ScanPluginFolder Then
            ScanPluginFolder()
        Else
            LoadSet()
        End If
        If My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\UCS\Ide") IsNot Nothing Then
            Label6.Text = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\UCS\Ide", "LibPath", "")
            ScanLibFolder()
        End If

        Label14.Text = m_res.MacroPath

        Label13.Text = m_res.ProjectPath

        Label7.Text = m_res.DataPath

        Label9.Text = m_res.SettingsPath

    End Sub
    Public Sub New(ByVal Resources As UCBase.ResourceManager)
        InitializeComponent()
        m_res = Resources
        CheckBox1.Checked = m_res.DevelopmentMode
        CheckBox6.Checked = m_res.ScanPluginFolder
        Label2.Text = m_res.PluginPath
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            m_res.PluginPath = FolderBrowserDialog1.SelectedPath
            Label2.Text = m_res.PluginPath
            m_res.SaveSettings(Application.StartupPath)
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If IO.Directory.Exists(m_res.SettingsPath) Then
            FolderBrowserDialog1.SelectedPath = m_res.SettingsPath
        Else
            FolderBrowserDialog1.SelectedPath = Application.StartupPath
        End If
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            m_res.SettingsPath = FolderBrowserDialog1.SelectedPath
            Label9.Text = m_res.SettingsPath
        End If
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Label6.Text = FolderBrowserDialog1.SelectedPath
            If My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\UCS\Ide") IsNot Nothing Then
                My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\UCS\Ide", "LibPath", FolderBrowserDialog1.SelectedPath)
            Else
                Dim newKey As RegistryKey
                newKey = My.Computer.Registry.LocalMachine.CreateSubKey("SOFTWARE\UCS\Ide")
                newKey.SetValue("LibPath", FolderBrowserDialog1.SelectedPath)
            End If
        End If
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        If IO.Directory.Exists(m_res.MacroPath) Then
            FolderBrowserDialog1.SelectedPath = m_res.MacroPath
        Else
            FolderBrowserDialog1.SelectedPath = Application.StartupPath
        End If
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            m_res.MacroPath = FolderBrowserDialog1.SelectedPath
            Label14.Text = m_res.MacroPath
        End If

    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        If IO.Directory.Exists(m_res.ProjectPath) Then
            FolderBrowserDialog1.SelectedPath = m_res.ProjectPath
        Else
            FolderBrowserDialog1.SelectedPath = Application.StartupPath
        End If
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            m_res.ProjectPath = FolderBrowserDialog1.SelectedPath
            Label13.Text = m_res.ProjectPath
        End If

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If IO.Directory.Exists(m_res.DataPath) Then
            FolderBrowserDialog1.SelectedPath = m_res.DataPath
        Else
            FolderBrowserDialog1.SelectedPath = Application.StartupPath
        End If
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            m_res.DataPath = FolderBrowserDialog1.SelectedPath
            Label7.Text = m_res.DataPath
        End If

    End Sub

    Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click

    End Sub
End Class